﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenSource.UPnP;
using System.IO;
using System.Collections;
using System.Reflection;

namespace ApplicationClienteCommunicante
{
    class ClientApplicationCommunicante
    {

        private int nb_dev;
        static ArrayList _zones = new ArrayList();
        private UPnPSmartControlPoint cp;

        UPnPArgument[] args2 = new UPnPArgument[2];
        private UPnPSmartControlPoint cp2;

        MainWindow mainwindow;

        public ClientApplicationCommunicante(MainWindow mainw)
        {
            nb_dev = 0;
            mainwindow = mainw; 
        }

        static public ArrayList Zones
        {
            get { return _zones; }
            set { _zones = value; }
        }


        string m;



       

        public void Search()
        {
            Console.WriteLine("/////////////////////////////////////////////////////////////////");

            m = MainWindow.nomdispo;
            Console.WriteLine("Nom du dispooooooooo est : " + MainWindow.nomdispo);
            Console.WriteLine("/////////////////////////////////////////////////////////////////");

            cp = new UPnPSmartControlPoint(new UPnPSmartControlPoint.DeviceHandler(NewDevice));
            cp.OnRemovedDevice += new UPnPSmartControlPoint.DeviceHandler(DeviceQuit);
            cp.Rescan();
        }




        /**
        * \NewDevice                pour la recherche d'un device sur le réseau.
         * \param    sender1        sender1 pour le sender qui a declencher la methode.
        * \param    device          device est le device qui va apparaitre sur réseau.
         */
        public void NewDevice(UPnPSmartControlPoint sender, UPnPDevice device)
        {


            if (device.FriendlyName != MainWindow.nomdispo)
            {
                Console.WriteLine("ignoring " + device.FriendlyName);
                return;
            }

            nb_dev++;

            _zones.Add(nb_dev);

            _zones.Add(MainWindow.nomdispo);

            Console.WriteLine(MainWindow.nomdispo + " found: " + nb_dev);
           }



        /**
        * \DeviceQuit               affiche un message si un device a quitté le réseau
        * \param    sender1         sender1 pour le sender qui a déclenché la méthode.
        * \param    device          device est le device qui va quitter le réseau.
        */

        public void DeviceQuit(UPnPSmartControlPoint sender, UPnPDevice device)
        {

            Console.Out.WriteLine(MainWindow.nomdispo + " quit: " + nb_dev);

            nb_dev--;
        }


        public void getParametreInterface(double width, double height) {


            Console.WriteLine("w" +width);
            Console.WriteLine("H" + height);
            string s4 = Convert.ToString(width);
            string s5 = Convert.ToString(height);

            args2[0] = new UPnPArgument("s4", s4);
            args2[1] = new UPnPArgument("s5", s5);

            Console.WriteLine("args2[0]"+args2[0]);
            Console.WriteLine("args2[1]" + args2[1]);
            cp2 = new UPnPSmartControlPoint(new UPnPSmartControlPoint.DeviceHandler(sendParametreInterface));
        
        }


        public void sendParametreInterface(UPnPSmartControlPoint sender, UPnPDevice device) {

            if (device.FriendlyName == MainWindow.nomdispo)
            {
                Console.WriteLine("trueeeeeeeeeeeeeeeee nchalah");

                UPnPService serv = device.GetService("Sample2");

                if (serv == null)
                {
                    Console.Error.WriteLine("Could not find service `Sample2' in device ");
                }

                Console.WriteLine("service  Sample2 is Found");

                string retval3=(string)serv.InvokeSync("recupereParametreInterface", args2);

                if (retval3 != null)
                {

                    Console.WriteLine("ya RAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBB");

                    Console.WriteLine("valeuuuuuuuuuuuuuuurrrrrrrrrrrrrrrrr de retval est : " + retval3);
                }

                mainwindow.RefreshMessage2(retval3);
            }

        
        
        }



    }
}
